﻿Imports System.Xml
Public Class frmFileRepositoryOpen



    Private Sub BtnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBuscar.Click
        FDLAbrir.Filter = "Archivo de repositorio(*" & g_StrRepositoryFileExtension & ")|*" & g_StrRepositoryFileExtension
        FDLAbrir.DefaultExt = g_StrRepositoryFileExtension
        FDLAbrir.ShowDialog()
        TxtRepo.Text = FDLAbrir.FileName
    End Sub

    Private Sub BtnAbrir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAbrir.Click
        'lee el archivo



        If TxtRepo.Text = "" Then
            MessageBox.Show("Seleccione el repositorio que desea abrir.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub
        End If

        Dim readFile As System.IO.TextReader = New System.IO.StreamReader(TxtRepo.Text)
        Try
            g_TituloRepositorio = readFile.ReadLine()
            g_TituloRepositorio = g_TituloRepositorio.Replace("Titulo=", "")

            g_CadenaConexionRepo = readFile.ReadLine()
            g_CadenaConexionRepo = g_CadenaConexionRepo.Replace("Ruta=", "")

            g_StrPathDocument = readFile.ReadLine()
            g_StrPathDocument = g_StrPathDocument.Replace("imagenes=", "")

            g_TipoBaseDatos = readFile.ReadLine()
            g_TipoBaseDatos = g_TipoBaseDatos.Replace("Provider=", "")

            Call GuardaHistorial(g_TituloRepositorio, TxtRepo.Text)
            MessageBox.Show("Conexión correcta al repositorio.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show("Error al intentar abrir el repositorio.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            readFile.Close()
            readFile = Nothing
        End Try




    End Sub
    Sub GuardaHistorial(ByVal StrRepositorio As String, ByVal StrPath As String)

        Dim Doc As New XmlDocument, Nodo As XmlNode
        Dim StrRepositorio1, StrPath1 As String

        Nodo = Doc.CreateElement("Historial")
        Doc.AppendChild(Nodo)

        Nodo = Doc.CreateElement("Repositorios")
        Nodo.InnerText = StrRepositorio & "|" & StrPath
        Doc.DocumentElement.AppendChild(Nodo)

        'Registra item del historial que estan en el listview

        If LvHistorial.Items.Count = 0 Then
            Doc.Save(Application.StartupPath & "\Historial.xml")
            Exit Sub
        End If

        Dim intaux As Byte
        For intaux = 0 To LvHistorial.Items.Count - 1
            StrRepositorio1 = LvHistorial.Items(intaux).Text
            StrPath1 = LvHistorial.Items(intaux).SubItems(1).Text

            If StrRepositorio <> StrRepositorio1 Then
                Nodo = Doc.CreateElement("Repositorios")
                Nodo.InnerText = StrRepositorio1 & "|" & StrPath1
                Doc.DocumentElement.AppendChild(Nodo)
            End If

        Next

        Doc.Save(Application.StartupPath & "\Historial.xml")

    End Sub

    Private Sub frmFileRepositoryOpen_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        Dim Doc As New XmlDocument, ListaNodos As XmlNodeList, Nodo As XmlNode
        Dim StrRepoNombre, StrRepoPath As String
        Dim IntAux As Byte
        Dim lviItem As ListViewItem
        Try
            Doc.Load(Application.StartupPath & "\Historial.xml")
            ListaNodos = Doc.SelectNodes("/Historial/Repositorios")
            For Each Nodo In ListaNodos
                IntAux = Nodo.ChildNodes.Item(0).InnerText.IndexOf("|")

                lviItem = New ListViewItem
                

                StrRepoNombre = Nodo.ChildNodes.Item(0).InnerText.Substring(0, IntAux)
                StrRepoPath = Nodo.ChildNodes.Item(0).InnerText.Substring(IntAux + 1)
                lviItem.Text = StrRepoNombre
                lviItem.SubItems.Add(StrRepoPath)

                LvHistorial.Items.Add(lviItem)
                ' LvHistorial.Items(LvHistorial.Items.Count - 1).SubItems(1).Text = StrRepoPath
            Next

        Catch ex As Exception

        End Try



    End Sub

    Private Sub BtnBorraHistorial_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBorraHistorial.Click
        LvHistorial.Items.Clear()
    End Sub

    Private Sub LvHistorial_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles LvHistorial.DoubleClick
        If LvHistorial.Items.Count > 0 Then
            TxtRepo.Text = LvHistorial.SelectedItems(0).SubItems(1).Text
        End If
    End Sub

    Private Sub LvHistorial_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LvHistorial.SelectedIndexChanged

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()
    End Sub
End Class